
// Example sketch which shows how to display some patterns
// on a 64x32 LED matrix
// 2023-11-10在mini esp32s2上测试通过 使用了64*64的p3单屏

#include <ESP32-HUB75-MatrixPanel-I2S-DMA.h>
#include <U8g2_for_Adafruit_GFX.h>


#define PANEL_RES_X 64  // 板x灯数量
#define PANEL_RES_Y 32  // 板y灯数量
#define PANEL_CHAIN 1   // 板数量

//MatrixPanel_I2S_DMA dma_display;
MatrixPanel_I2S_DMA *dma_display = nullptr;
// #include <U8g2_for_Adafruit_GFX.h>
U8G2_FOR_ADAFRUIT_GFX u8g2Fonts;

uint16_t myBLACK = dma_display->color565(0, 0, 0);
uint16_t myWHITE = dma_display->color565(255, 255, 255);
uint16_t myRED = dma_display->color565(255, 0, 0);
uint16_t myGREEN = dma_display->color565(0, 255, 0);
uint16_t myBLUE = dma_display->color565(0, 0, 255);



// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
// From: https://gist.github.com/davidegironi/3144efdc6d67e5df55438cc3cba613c8
uint16_t colorWheel(uint8_t pos) {
  if (pos < 85) {
    return dma_display->color565(pos * 3, 255 - pos * 3, 0);
  } else if (pos < 170) {
    pos -= 85;
    return dma_display->color565(255 - pos * 3, 0, pos * 3);
  } else {
    pos -= 170;
    return dma_display->color565(0, pos * 3, 255 - pos * 3);
  }
}

void drawText(int colorWheelOffset) {

  // draw text with a rotating colour
  dma_display->setTextSize(1);      // size 1 == 8 pixels high
  dma_display->setTextWrap(false);  // Don't wrap at end of line - will do ourselves

  dma_display->setCursor(5, 0);  // start at top left, with 8 pixel of spacing
  uint8_t w = 0;
  const char *str = "-ESP32S2-";
  for (w = 0; w < strlen(str); w++) {
    dma_display->setTextColor(colorWheel((w * 32) + colorWheelOffset));
    dma_display->print(str[w]);
  }

  dma_display->println();
  dma_display->println();

  dma_display->print(" ");
  for (w = 9; w < 18; w++) {
    dma_display->setTextColor(colorWheel((w * 32) + colorWheelOffset));
    dma_display->print("*");
  }

  dma_display->println();
  dma_display->println();

  dma_display->setTextColor(dma_display->color444(15, 15, 15));
  dma_display->println("LED-HONGSHU");
  dma_display->println();


  // print each letter with a fixed rainbow color
  dma_display->setTextColor(dma_display->color444(0, 8, 15));
  dma_display->print('6');
  dma_display->setTextColor(dma_display->color444(15, 4, 0));
  dma_display->print('4');
  dma_display->setTextColor(dma_display->color444(15, 15, 0));
  dma_display->print('x');
  dma_display->setTextColor(dma_display->color444(8, 15, 0));
  dma_display->print('6');
  dma_display->setTextColor(dma_display->color444(8, 0, 15));
  dma_display->print('4');
  dma_display->println();

  // Jump a half character
  dma_display->setCursor(34, 24 + 8 + 8 + 8);
  dma_display->setTextColor(dma_display->color444(0, 15, 15));
  dma_display->print("*");
  dma_display->setTextColor(dma_display->color444(15, 0, 0));
  dma_display->print('R');
  dma_display->setTextColor(dma_display->color444(0, 15, 0));
  dma_display->print('G');
  dma_display->setTextColor(dma_display->color444(0, 0, 15));
  dma_display->print("B");
  dma_display->setTextColor(dma_display->color444(15, 0, 8));
  dma_display->println("*");
}


void setup() {

  // Module configuration
  HUB75_I2S_CFG mxconfig(
    PANEL_RES_X,  // module width
    PANEL_RES_Y,  // module height
    PANEL_CHAIN   // Chain length
  );

  // 14pin
  mxconfig.gpio.r1 = 9;
  mxconfig.gpio.g1 = 10;
  // mxconfig.gpio.b1 = 11;
  mxconfig.gpio.r2 = 12;
  mxconfig.gpio.g2 = 13;
  // mxconfig.gpio.b2 = 14;
  mxconfig.gpio.a = 16;
  mxconfig.gpio.b = 17;
  mxconfig.gpio.c = 21;
  mxconfig.gpio.d = 18;
  // mxconfig.gpio.e = 5;
  mxconfig.gpio.clk = 6;
  mxconfig.gpio.oe = 7;
  mxconfig.gpio.lat = 8;

  //mxconfig.gpio.e = 18;
  // mxconfig.clkphase = false;
  // mxconfig.driver = HUB75_I2S_CFG::FM6126A;

  // Display Setup
  dma_display = new MatrixPanel_I2S_DMA(mxconfig);
  dma_display->begin();
  dma_display->setBrightness8(90);  //0-255
  dma_display->clearScreen();
  dma_display->fillScreen(myWHITE);

  // fix the screen with green
  dma_display->fillRect(0, 0, dma_display->width(), dma_display->height(), dma_display->color444(0, 15, 0));
  delay(500);

  // draw a box in yellow
  dma_display->drawRect(0, 0, dma_display->width(), dma_display->height(), dma_display->color444(15, 15, 0));
  delay(500);

  // draw an 'X' in red
  dma_display->drawLine(0, 0, dma_display->width() - 1, dma_display->height() - 1, dma_display->color444(15, 0, 0));
  dma_display->drawLine(dma_display->width() - 1, 0, 0, dma_display->height() - 1, dma_display->color444(15, 0, 0));
  delay(500);

  // draw a blue circle
  dma_display->drawCircle(10, 10, 10, dma_display->color444(0, 0, 15));
  delay(500);

  // fill a violet circle
  dma_display->fillCircle(40, 21, 10, dma_display->color444(15, 0, 15));
  delay(500);

  // fill the screen with 'black'
  dma_display->fillScreen(dma_display->color444(0, 0, 0));

  // drawText(0);

  // arduino ide 安装库 U8g2_for_Adafruit_GFX  引入库 #include <U8g2_for_Adafruit_GFX.h>
  u8g2Fonts.begin(*dma_display);
  u8g2Fonts.setFontMode(1);
  u8g2Fonts.setFontDirection(0);
  u8g2Fonts.setFont(u8g2_font_wqy16_t_gb2312);
}

uint8_t wheelval = 0;
void loop() {

  // animate by going through the colour wheel for the first two lines
  drawText(wheelval);
  // wheelval +=1;

  delay(3000);

  // drawText(0);
  delay(1000);
  dma_display->clearScreen();
  dma_display->fillScreen(myBLACK);
  delay(1000);
  dma_display->fillScreen(myBLUE);
  delay(1000);
  dma_display->fillScreen(myRED);
  delay(1000);
  dma_display->fillScreen(myGREEN);
  delay(1000);
  dma_display->fillScreen(myWHITE);
  delay(1000);
  dma_display->clearScreen();
  delay(500);

  u8g2Fonts.setForegroundColor(dma_display->color565(random(256), random(256), random(256)));
  int16_t s = u8g2Fonts.getUTF8Width("记得要三连哦！");
  for (size_t i = 0; i < s; i++) {
    u8g2Fonts.drawUTF8(-i, 16, "记得要三连哦！");
    delay(100);
    dma_display->clearScreen();
  }
  // for (size_t i = s; i >= 0; i--)
  // {
  //   u8g2Fonts.drawUTF8(-i,32,"！吧的要你是");
  //   delay(100);
  //   dma_display->clearScreen();
  // }
}
